import logging

from eshop.lib.base import *
from property import DATA_TYPE_OPTIONS
from authkit.authorize.pylons_adaptors import authorize
from authkit.permissions import RemoteUser, ValidAuthKitUser

#from model import *

log = logging.getLogger(__name__)

class CategoryController(BaseController):

    def list(self):
        c.categories = model.ProductCategory.q.all()
#        c.categories = model.ProductCategory.q.filter_by(parent_category_id=None)
        return render('/catalog/category_list.mako')
    
    def add(self):
        c.categories = model.ProductCategory.q.all()
        c.parent_category_id = request.params.get('parent_category_id')
        return render('/catalog/category_add.mako')
    
    def delete(self, id):
        model.ProductCategory.q.get(id).delete()
        redirect_to(action='index', id=None)
    
    def edit (self, id):
        c.category = model.ProductCategory.q.get(id)
        c.path = self.__calculate_path(c.category)
        c.categories = model.ProductCategory.q.all()
        return render('/catalog/category_edit.mako')

    @authorize(RemoteUser())
    def index(self, id=None):
        return self.view(id)
    
    def view (self, id=None):
        if id != None:
            c.category = model.ProductCategory.q.get(id)
            c.path = self.__calculate_path(c.category)
            c.child_categories = c.category.child_categories
            c.data_types = DATA_TYPE_OPTIONS
            c.properties = c.category.properties
            c.all_properties = model.ProductProperty.q.all()
            c.products = c.category.products
        else:
            c.path = []
            c.child_categories = model.ProductCategory.q.filter_by(parent_category_id=None)
        print c.category
        print c.path
        print c.child_categories    
        return render('/catalog/category_view.mako')
    
    def save_edit(self, id):
        category = model.ProductCategory.q.get(id)
        category.name=request.params.get('name')
        category.description=request.params.get('description')
        parent_category_id = request.params.get('parent_category')
        if parent_category_id != '':
            category.parent_category = model.ProductCategory.q.get(parent_category_id)
        else:
            category.parent_category = None
        redirect_to(action='index')
        
    def save_add(self):
        category = model.ProductCategory(\
            name=request.params.get('name'),\
            description=request.params.get('description'))
        parent_category_id = request.params.get('parent_category')
        if parent_category_id != '':
            parent_category = model.ProductCategory.q.get(parent_category_id)
            parent_category.child_categories.append(category)
            category.parent_category = parent_category
        redirect_to(action='index')
    
    def save_add_property(self, id):
        property = model.ProductProperty.q.get(request.params.get('property_code'))
        category = model.ProductCategory.q.get(id)
        category.properties.append(property)
        redirect_to(action='view')
    
    def delete_property(self, id):
        property = model.ProductProperty.q.get(request.params.get('property_code'))
        model.ProductCategory.q.get(id).properties.remove(property)
        redirect_to(action='view')
             
    def __calculate_path(self, category, include_current=False):
        if category == None:
            return [] 
        result = [category] if include_current else []
        while category.parent_category != None:
            result.append(category.parent_category)
            category = category.parent_category
        return result 